package com.wwdx.service.impl;

import com.google.common.collect.Lists;
import com.wwdx.BaseCommons;
import com.wwdx.model.WwdxBookChapterModel;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.model.WwdxThemeModel;
import com.wwdx.model.dto.WwdxBookBusinessDTO;
import com.wwdx.model.dto.WwdxThemeBusinessDTO;
import com.wwdx.service.ProductServiceI;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 产品业务类
 * @author liguoliang
 * @date 2018/7/31 10:37
 */
@Service
public class ProductServiceImpl extends BaseCommons implements ProductServiceI {

    @Override
    public List<WwdxThemeBusinessDTO> themeList(List<WwdxThemeModel> themeModels, String unionid){
        List<WwdxThemeBusinessDTO> list = Lists.newArrayListWithExpectedSize(themeModels.size());
        themeModels.forEach(wwdxThemeModel -> list.add(this.execute(unionid,wwdxThemeModel)));
        return list;
    }

    private WwdxThemeBusinessDTO execute(String unionid, WwdxThemeModel wwdxThemeModel){
        WwdxThemeBusinessDTO dto = new WwdxThemeBusinessDTO();
        BeanUtils.copyProperties(wwdxThemeModel,dto);
        String[] bookList = wwdxThemeModel.getBook_list().split(",");
        //书籍数量
        dto.setBook_count((long)bookList.length);
        long themeid = wwdxThemeModel.getThemeid();
        //是否为书架
        dto.setIsbookshelf(wwdxOrderService.isbookshelf(themeid,unionid));
        //是否购买
        dto.setIsorder(wwdxOrderService.isorder(themeid,unionid));
        return dto;
    }

    @Override
    public WwdxThemeBusinessDTO themeDetail(WwdxThemeModel model, String unionid){
        WwdxThemeBusinessDTO dto = this.execute(unionid, model);
        //书单列表
        WwdxBookModel wwdxBookModel = WwdxBookModel.builder().themeid(model.getThemeid()).status(0).type(0).build();
        dto.setThemebook_list(wwdxBookService.selectModelList(wwdxBookModel,1,50));
        //猜你喜欢(只有精品课才有)
        if (model.getType() == 0){
            model = WwdxThemeModel.builder().type(0).status(0).random(true).build();
            dto.setTheme_recommend(this.themeList(wwdxThemeService.selectModelList(model,1,2),unionid));
        }
        return dto;
    }

    @Override
    public List<WwdxBookBusinessDTO> bookList(WwdxBookModel wwdxBookModel,String unionid,Integer page, Integer rows){
        return this.bookList(wwdxBookService.selectModelList(wwdxBookModel,page,rows),unionid);
    }

    @Override
    public List<WwdxBookBusinessDTO> bookList(List<WwdxBookModel> bookModels,String unionid){
        List<WwdxBookBusinessDTO> wwdxBookBusinessDTOS = Lists.newArrayListWithExpectedSize(bookModels.size());
        bookModels.forEach(wwdxBookModel-> wwdxBookBusinessDTOS.add(this.execute(unionid,wwdxBookModel)));
        return wwdxBookBusinessDTOS;
    }

    private WwdxBookBusinessDTO execute(String unionid,WwdxBookModel wwdxBookModel){
        WwdxBookBusinessDTO dto = new WwdxBookBusinessDTO();
        BeanUtils.copyProperties(wwdxBookModel,dto);
        //是否购买
        dto.setIsorder(wwdxOrderService.isorder(wwdxBookModel.getBookid(), unionid));
        //音频转述师
        dto.setVideoauthorInfo(wwdxCopywriterService.getById(wwdxBookModel.getVideoauthorid()));
        return dto;
    }

    @Override
    public WwdxBookBusinessDTO bookDetail(Long bookid, String unionid){
        WwdxBookModel wwdxBookModel = wwdxBookService.getByBookId(bookid);
        WwdxBookBusinessDTO dto = this.execute(unionid,wwdxBookModel);
        List<WwdxBookChapterModel> bookChapterList = wwdxBookChapterService.findBookChapterList(bookid);
        //章节列表
        dto.setBookchapter_list(bookChapterList);
        //章节数量
        dto.setChaptercount((long) bookChapterList.size());
        //书籍下才有撰稿人
        Integer type = wwdxBookModel.getType();
        if (type == 0){
            //撰稿人
            dto.setCopywriteInfo(wwdxCopywriterService.getById(wwdxBookModel.getCopywriter()));
        }
        //是否为书架书籍
        dto.setIsbookshelf(wwdxOrderService.isbookshelf(bookid,unionid));
        //猜你喜欢
        wwdxBookModel = WwdxBookModel.builder().status(0).type(0).random(true).build();
        dto.setBookrecommend_list(this.bookList(wwdxBookService.selectModelList(wwdxBookModel,1,2),unionid));
        return dto;
    }

    private WwdxBookBusinessDTO execute(WwdxBookModel wwdxBookModel){
        WwdxBookBusinessDTO dto = new WwdxBookBusinessDTO();
        BeanUtils.copyProperties(wwdxBookModel,dto);
        //音频转述师
        dto.setVideoauthorInfo(wwdxCopywriterService.getById(wwdxBookModel.getVideoauthorid()));
        return dto;
    }
    @Override
    public WwdxBookBusinessDTO bookDetail(Long bookid) {
        WwdxBookModel wwdxBookModel = wwdxBookService.getByBookId(bookid);
        WwdxBookBusinessDTO dto = this.execute(wwdxBookModel);
        return dto;
    }
}